package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;

import java.io.*;

public class ArticleController {
    private  static  String articlesDir="./articles/";
    static {
        File file=new File(articlesDir);
        if(!file.exists()){
            file.mkdirs();
        }
    }
    public void writeArticle(HttpRequest request, HttpResponse response){
        System.out.println("发表文章开始了");
        String title=request.getParameter("title");
        String author=request.getParameter("author");
        String Articles=request.getParameter("Articles");
        if(title==null||author==null||Articles==null){
            response.setEntity(new File("./webapps/myweb/article_fail.html"));
            System.out.println("文章发表失败");
            return;
        }
        Article articles=new Article(title,author,Articles);
        try (
                ObjectOutputStream op=new ObjectOutputStream(new FileOutputStream(articlesDir+title+".obj"))
                ){
            op.writeObject(articles);
            response.setEntity(new File("./webapps/myweb/article_success.html"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        System.out.println("发表文章结束了");

    }
}
